package com.xinguan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinguan.entity.AllInformation;
import com.xinguan.entity.Information;
import com.xinguan.entity.ItemDTO;
import com.xinguan.mapper.InformationMapper;
import com.xinguan.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 陈俊行
 * @Date: 2023/11/10/9:04
 * @Description:
 */
@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information> implements InformationService {
     @Autowired
     private InformationMapper informationMapper;
    //        PageHelper.startPage(1, 6);
//        List<AllInformation> informationList = informationMapper.selectALLInformation();
//        PageInfo<AllInformation> pageInfo = new PageInfo<>(informationList);
//        return pageInfo;
//        PageHelper.startPage(1, 6);
//        List<Map<String, Object>> resultList = informationMapper.selectALLInformation(); // 修改这里的查询方法名
//
//        // 使用 Jackson ObjectMapper 将 JSON 字符串转换为 List<ItemDTO>
//        ObjectMapper objectMapper = new ObjectMapper();
//        TypeReference<List<ItemDTO>> typeReference = new TypeReference<List<ItemDTO>>() {};
//
//        List<AllInformation> informationList = resultList.stream()
//                .map(result -> {
//                    AllInformation allInformation = new AllInformation();
//                    // 设置其他字段
//
//                    // 设置 items 字段
//                    String itemsJson = (String) result.get("items");
//                    List<ItemDTO> itemsList = null;
//                    try {
//                        itemsList = objectMapper.readValue(itemsJson, typeReference);
//                    } catch (JsonProcessingException e) {
//                        throw new RuntimeException(e);
//                    }
//                    allInformation.setItems(itemsList);
//
//                    return allInformation;
//                })
//                .collect(Collectors.toList());
//
//        PageInfo<AllInformation> pageInfo = new PageInfo<>(informationList);
//        return pageInfo;
    @Override
    public PageInfo<AllInformation> getAllInformation(String orderStatus) {
        // 分页，从第一页开始，每页显示6条记录
        PageHelper.startPage(1, 6);

// 查询数据库获取订单信息列表
        List<AllInformation> resultList = informationMapper.selectALLInformation(orderStatus);

// 遍历订单信息列表，将JSON字符串转换为Java对象
        for (AllInformation info : resultList) {
            // 获取items字段的JSON字符串
            String itemsString = info.getItems();

            try {
                // 使用Jackson ObjectMapper将JSON字符串转换为List<ItemDTO>对象
                ObjectMapper objectMapper = new ObjectMapper();
                List<ItemDTO> itemsList = objectMapper.readValue(itemsString, new TypeReference<List<ItemDTO>>() {});

                // 设置转换后的商品信息列表到订单信息对象中
                info.setItemsList(itemsList);
            } catch (JsonProcessingException e) {
                e.printStackTrace(); // 处理JSON解析异常，可以根据实际需求进行适当处理
            }
        }

        PageInfo<AllInformation> pageInfo = new PageInfo<>(resultList);

        return pageInfo;
    }

    @Override
    public void updateInformation(String operate,String orderId) {
        LambdaQueryWrapper<Information> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(Information::getOrderId,orderId);
        Information information = informationMapper.selectOne(wrapper);
        System.out.println(information);
        if(operate.equals("发货")){
            information.setOrderStatus("配送中");
        } else if (operate.equals("完成订单")) {
            information.setOrderStatus("已成交");
        }else {
            information.setOrderStatus("已退款");
        }
        informationMapper.updateById(information);
    }

    @Override
    public PageInfo<AllInformation> getSomeInformation(String orderId, Integer phone, LocalDateTime beginTime, LocalDateTime endTime) {
        // 分页，从第一页开始，每页显示6条记录
        PageHelper.startPage(1, 6);

        List<AllInformation> resultList = informationMapper.selectSomeInformation(orderId,phone,beginTime,endTime);

// 遍历订单信息列表，将JSON字符串转换为Java对象
        for (AllInformation info : resultList) {
            // 获取items字段的JSON字符串
            String itemsString = info.getItems();

            try {
                // 使用Jackson ObjectMapper将JSON字符串转换为List<ItemDTO>对象
                ObjectMapper objectMapper = new ObjectMapper();
                List<ItemDTO> itemsList = objectMapper.readValue(itemsString, new TypeReference<List<ItemDTO>>() {});

                // 设置转换后的商品信息列表到订单信息对象中
                info.setItemsList(itemsList);
            } catch (JsonProcessingException e) {
                e.printStackTrace(); // 处理JSON解析异常，可以根据实际需求进行适当处理
            }
        }

        PageInfo<AllInformation> pageInfo = new PageInfo<>(resultList);
        return pageInfo;
    }
}
